import hashlib
import time
import datetime
import requests
import json
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
import base64


class cmzs:
    def __init__(self, date, timestamp, md5, device_id, account, password, refund_password):
        self.date = date
        self.timestamp = timestamp
        self.md5 = md5
        self.device_id = device_id
        self.account = account
        self.password = password
        self.refund_password = refund_password
        self.config_r2 = None
        self.token = None
        self.uid = None
        self.req_msg_id = '123'
        self.sign = None
        self.suid = None
        self.code_id = None
        self.type1 = 1
        self.type2 = 2
        self.rsa_private = None
        self.strings = None
        self.rsa_private = None

    def private_key_signature(self):
        rsakey = RSA.importKey(self.rsa_private)
        signer = Signature_pkcs1_v1_5.new(rsakey)
        digest = SHA.new()
        digest.update(self.strings.encode("utf8"))
        sign = signer.sign(digest)
        signature1 = base64.b64encode(sign)
        signature2 = signature1.decode('utf-8')
        return signature2

    def config(self):  # 拉取配置，获取配置列表
        config_url = 'http://open.dairphone.cn/device/config_list'
        config_data = {
            "sn": self.device_id,
            "version": "2.2.0",
            "time": self.timestamp,
            "check_code": self.md5
        }
        config_r1 = requests.post(url=config_url, data=config_data)
        self.config_r2 = config_r1.json()
        # print(json.dumps(self.config_r2, indent=4, separators=(',', ':'), ensure_ascii=False))

    def plug_login(self):  # 登录接口
        privateKey = 'MIICXQIBAAKBgQCIZXObErQnhu7jduBjUdZVxajwXXQ5doem3QnpO2BmIozwAZW3f9OJgrXffyKPAzOwtY0raBpWenQmGH50PXntPXhC4444kCybOHBMGMYYNsY75kJ3X0imqRlsBfp8PZdBtFb2aSZAcv3FfDwG+J/ieLIGfNQ0bmlMGsAM3vdtRwIDAQABAoGAHL/1rDw3CRXiCMQn7Ad1/EJbp76rHumjEkMJMcY4PY8O0hAjdLhW6GdK2r87k5WDMb4y8XFSC+Rn0xfan7nNlytD0mwY9RDo72MrSHAr27q/cCnnmn8wOyzK33PMbrzWmQGoOVAh0UAvZstf4W0K6WdSl36Mdoi8VpeHJeqMhnECQQCzPs7K+QyKjZCrwgEqbC1QxlTZ7hETDqGUUTyOLDfRRkOP4NvfBCFhFMDXf1YioPhK0W4crqu5OijSTmE0jhd7AkEAws10itMx/d/4uErlDXq4jfOLGtzvdNXK3zlMvGm9fLX+npnaMK1htfFW37FyEdj9fDF9jqmcCGnShfmOk39xpQJAKn8vpCvb71WVAUPj5tzy+X2sTGcHIU+Mu49vHf4Z1cIk7o3PU1a1W0ECX5VPJLKpa4Ov6Zj4GdhSKVpv7Xfq1QJBAPmjM1NofKscJ298wvW4K+P1k3EUamUuSPIxSu41D5hO3gI/pYY12n6/zZWSn1zR114dSgrVvlhlzFzWR2krq+UCQQCbGX5m4OXjoAF2P4bzDHkBR7eYvMN215NIi+bKl/qx//GUsSCMOVTGiRFvstZej4fLFyEuFyfu6gFOT9VXBLmV'
        self.rsa_private = '-----BEGIN PRIVATE KEY-----\n' + privateKey + '\n-----END PRIVATE KEY-----'
        self.strings = 'account=' + self.account + '&device_id=' + self.device_id + '&password=' + self.password + \
                       '&req_msg_id=' + self.req_msg_id + '&sn=' + self.device_id + '&version=2.3.0'
        new_sign = cmzs.private_key_signature()
        plug_login_url = self.config_r2['data']['plug_login']['url']
        # print(plug_login_url)
        # 获取登录接口地址
        plug_login_data = {
            'device_id': self.device_id,
            'req_msg_id': self.req_msg_id,
            'sn': self.device_id,
            'sign': new_sign,
            'version': '2.3.0',
            'account': self.account,
            'password': self.password
        }
        # strings = 'account=18680352162&device_id=99001100000824&password=123456&req_msg_id=123&sn' \
        #           '=99001100000824&version=2.3.0'
        print(self.strings)
        plug_login_r1 = requests.post(url=plug_login_url, data=plug_login_data)
        plug_login_r2 = plug_login_r1.json()
        print(json.dumps(plug_login_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        # 返回登录接口参数
        self.token = plug_login_r2['data']['token']
        self.rsa_private = plug_login_r2['data']['rsa_private']
        print(self.token)
        print(self.rsa_private)
        # 获取token

    def plug_shop_list(self):  # 门店接口
        self.strings = 'account=' + self.account + '&device_id=' + self.device_id + '&password=' + self.password + \
                       '&req_msg_id=' + self.req_msg_id + '&sn=' + self.device_id + '&version=2.3.0'

        plug_shop_list_url = self.config_r2['data']['plug_shop_list']['url']
        print(plug_shop_list_url)
        # 获取门店接口地址
        plug_shop_list_data = {
            'token': self.token,
            'req_msg_id': self.req_msg_id,
            'sign': self.rsa_private
        }
        plug_shop_list_r1 = requests.post(url=plug_shop_list_url, data=plug_shop_list_data)
        plug_shop_list_r2 = plug_shop_list_r1.json()
        print(json.dumps(plug_shop_list_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        # 返回门店接口参数
        self.suid = plug_shop_list_r2['data']['shop_sub_id']
        print(self.suid)
        # 获取suid
    #
    # def plug_code_list(self):  # 收银点接口
    #     plug_code_list_url = self.config_r2['data']['plug_code_list']['url']
    #     print(plug_code_list_url)
    #     # 获取收银点接口地址
    #     plug_code_list_data = {
    #         'suid': self.suid,
    #         'token': self.token,
    #         'req_msg_id': self.req_msg_id,
    #         'sign': self.sign
    #     }
    #     plug_code_list_r1 = requests.post(url=plug_code_list_url, data=plug_code_list_data)
    #     plug_code_list_r2 = plug_code_list_r1.json()
    #     print(json.dumps(plug_code_list_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
    #     # 返回收银点接口参数
    #     self.code_id = plug_code_list_r2['data']['code_id']
    #     self.uid = plug_code_list_r2['data']['last_uid']
    #
    # def plug_onduty(self):  # 交接班接口
    #     # 下班签退
    #     plug_onduty_url1 = self.config_r2['data']['plug_onduty']['url']
    #     print(plug_onduty_url1)
    #     # 获取交接班接口地址
    #     plug_onduty_data1 = {
    #         'token': self.token,
    #         'type': self.type2,
    #         'code_id': self.code_id,
    #         'suid': self.suid,
    #         'req_msg_id': self.req_msg_id,
    #         'sign': self.sign
    #     }
    #     plug_onduty_r1 = requests.post(url=plug_onduty_url1, data=plug_onduty_data1)
    #     plug_onduty_r2 = plug_onduty_r1.json()
    #     print(json.dumps(plug_onduty_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
    #     # 返回交接接口参数
    #     # 上班签到
    #     plug_onduty_url2 = self.config_r2['data']['plug_onduty']['url']
    #     print(plug_onduty_url2)
    #     # 获取交接班接口地址
    #     plug_onduty_data2 = {
    #         'token': self.token,
    #         'type': self.type1,
    #         'code_id': self.code_id,
    #         'suid': self.suid,
    #         'req_msg_id': self.req_msg_id,
    #         'sign': self.sign
    #     }
    #     plug_onduty_r3 = requests.post(url=plug_onduty_url2, data=plug_onduty_data2)
    #     plug_onduty_r4 = plug_onduty_r3.json()
    #     print(json.dumps(plug_onduty_r4, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))

    # def plug_code_list(self):  # 收银点接口
    #     plug_code_list_url = self.config_r2['data']['plug_onduty']['url']
    #     print(plug_code_list_url)
    #     plug_code_list_data = {
    #         'sign': self.sign,
    #         'req_msg_id': self.req_msg_id,
    #         'token': self.token,
    #         'suid': self.suid
    #     }
    #     plug_code_list_r1 = requests.post(url=plug_code_list_url, data=plug_code_list_data)
    #     plug_code_list_r2 = plug_code_list_r1.json()
    #     print(json.dumps(plug_code_list_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
    #     # 返回收银点接口参数


date1 = datetime.datetime.now().strftime("%Y-%m-%d")
timestamp1 = time.time()
a = str(timestamp1) + 'CMJF_getSignInfo'
md5 = hashlib.md5()
md5.update(a.encode('utf-8'))
md51 = md5.hexdigest()
# print(timestamp1)
# print(md51)
device_id1 = str(input("请输入要测试的设备编号(99001100000824)："))
account1 = str(input("请输入商户/员工/店长/财务账号(18680352162)："))
password1 = str(input("请输入商户/员工/店长/财务密码(123456)："))
refund_password1 = str(input("请输入退款密码(123456)："))
cmzs = cmzs(date1, timestamp1, md51, device_id1, account1, password1, refund_password1)
cmzs.config()
cmzs.plug_login()
